package rs.in.idemdo;

import java.util.ArrayList;

import rs.in.idemdo.db.IdemDoDbAdapter;
import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.ToggleButton;

public class IzborMestaActivity extends Activity {

	protected static final int VREME_DIJALOG_ID = 0;
	private IdemDoDbAdapter mIdemDoDbAdapter;

	// the callback received when the user "sets" the date in the dialog
	private TimePickerDialog.OnTimeSetListener mPostaviVreme = new TimePickerDialog.OnTimeSetListener() {

		@Override
		public void onTimeSet(TimePicker arg0, int arg1, int arg2) {
			final TextView vreme = (TextView) findViewById(R.id.txv_vreme_od);
			vreme.setText(arg1 + ":" + arg2);
		}
	};

	@Override
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.izbor_mesta);

		mIdemDoDbAdapter = new IdemDoDbAdapter(this);
		final IdemDoDbAdapter idemDoDbAdapter = mIdemDoDbAdapter;

		idemDoDbAdapter.obrisiSvaMesta();
		idemDoDbAdapter.open();

		idemDoDbAdapter.createMesto("Beograd", "BG");
		idemDoDbAdapter.createMesto("Backa Palanka", "BP");
		idemDoDbAdapter.createMesto("Becej", "BE");
		idemDoDbAdapter.createMesto("Begec", "BC");
		idemDoDbAdapter.createMesto("Bac", "BA");
		idemDoDbAdapter.createMesto("Zrenjanin", "ZR");

		final ArrayList<String> naziviMesta = new ArrayList<String>();
		final Cursor c = idemDoDbAdapter.fetchAllMesta();
		final int column_index = c.getColumnIndex(IdemDoDbAdapter.MESTO_NAZIV);

		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
			String column_string = c.getString(column_index);
			naziviMesta.add(column_string);
		}

		ArrayAdapter<String> adapterOd = new ArrayAdapter<String>(this,
				R.layout.izbor_mesta_auto_stavka, naziviMesta);
		AutoCompleteTextView tekstPoljeOd = (AutoCompleteTextView) findViewById(R.id.actv_od);

		ArrayAdapter<String> adapterDo = new ArrayAdapter<String>(this,
				R.layout.izbor_mesta_auto_stavka, naziviMesta);
		AutoCompleteTextView tekstPoljeDo = (AutoCompleteTextView) findViewById(R.id.actv_do);

		tekstPoljeOd.setAdapter(adapterOd);
		tekstPoljeDo.setAdapter(adapterDo);

	}

	public void vremeDijalog(View v) {
		showDialog(VREME_DIJALOG_ID);
	}

	public void pretrazi(View view) {
		final AutoCompleteTextView mestoOd = (AutoCompleteTextView) findViewById(R.id.actv_od);
		final AutoCompleteTextView mestoDo = (AutoCompleteTextView) findViewById(R.id.actv_do);

		Intent intentRezultatiPretrage = new Intent(this,
				RezultatiPretrageActivity.class);
		intentRezultatiPretrage.putExtra("mesto_od", mestoOd.getText());
		intentRezultatiPretrage.putExtra("mesto_do", mestoDo.getText());
		// TODO: povlastica informacija treba
		intentRezultatiPretrage.putExtra("povlastice", "");

		startActivity(intentRezultatiPretrage);
	}

	public void omoguciMestoPresedanja(View view) {
		final ToggleButton mp = (ToggleButton) findViewById(R.id.tb_mesto_presedanja);
		final LinearLayout ll = (LinearLayout) findViewById(R.id.ll_mesto_presedanja);
		if (mp.isChecked()) {
			ll.setVisibility(View.VISIBLE);
		} else {
			ll.setVisibility(View.GONE);
		}
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case VREME_DIJALOG_ID:
			return new TimePickerDialog(this, mPostaviVreme, 12, 0, true);
		}
		return null;
	}
}
